class ApplicationController < ActionController::API
	before_action :decodeURIParameters
	include ActionController::HttpAuthentication::Token::ControllerMethods
	
	def authenticate
		sid = getSessionId
		@user = PhoneUser.where( api_key: sid ).first or session_not_found( sid )
	end
	
	def not_found
	  raise ActionController::RoutingError.new('Not Found')
	end
	
	def session_not_found(sessionid)
	  raise ActionController::RoutingError.new('Not Found session='+sessionid)
	end
	
	def getSessionId
		sid = request.headers["sessionid"]
		if sid
			return sid
		end
		sid = params["sessionid"]
		if sid
			return sid
		end
		return nil
	end

	def decodeURIParameters
		paramString = "PARAMS: "
		params.each do |key,value|
			
			if value.is_a? String
				params[key] = URI.decode(value)
				paramString +=  ( ""  + key  +  "."  + value + " | " )
			end
		end
		puts paramString
	end
end
